package aiP.A3.Client.Monitor;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Andreas
 * Date: 26.05.14
 * Time: 12:19
 * To change this template use File | Settings | File Templates.
 */
public class DeadInstanceThread extends Thread {

    private Monitor monitor;
    private Date now;                                                           // Zeitstempel für Log Ausgaben
    private SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss");  // Zeitstempel formatieren


    public DeadInstanceThread(Monitor self){
        monitor = self;
    }


    public void run() {
        while (!this.isInterrupted()) {
            try {
                sleep(Monitor.INSTANCE_CHECK_TIME);
                setToteInstanzen();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (RemoteException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (NotBoundException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }
    }

    private void setToteInstanzen() throws RemoteException, NotBoundException {
        for (Instance instance : getToteInstanzen()) {
            instance.setTot();
        }
    }

    private List<Instance> getToteInstanzen() throws RemoteException, NotBoundException {
        List<Instance> erg = new ArrayList<Instance>();
        for (Instance instance : monitor.getInstanzen()) {
            if (!instance.isLebendig() && instance.getZuletztOnlineInMilli() > Monitor.INSTANCE_CHECK_TIME && !deadInstance.contains(instance)){
                erg.add(instance);
                System.err.println(instance.getId() + " ist gestorben :(");
                now = new Date();
                deadInstance.add(instance);
            }
        }
        return erg;
    }


    private Set<Instance> deadInstance = new HashSet<Instance>();

    public void clearInstance(Instance instance){
        deadInstance.remove(instance);
    }

//     Alt
//    public void run() {
//        while (true) {
//            try {
//                sleep(Monitor.INSTANCE_CHECK_TIME);
//                setToteInstanzen();
//
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    private void setToteInstanzen() {
//        for (Instance instance : getToteInstanzen()) {
//            instance.setTot();
//        }
//    }
//
//    private List<Instance> getToteInstanzen() {
//        List<Instance> erg = new ArrayList<Instance>();
//        for (Instance instance : monitor.getInstanzen()) {
//            if (instance.getZuletztOnlineInMilli() > Monitor.INSTANCE_CHECK_TIME) {
//                erg.add(instance);
//                System.out.println("hier ist was gestorben :(");
//                now = new Date();
//                monitor.getDashboard().logMessage(dateFormatter.format(now) + " " + instance.getId() + " ist gestorben :(");
//                monitor.removeInstanzByID(instance.getId());
//
//            }
//        }
//        return erg;
//    }


}
